var app = angular.module('app');

app.controller('bookUpdateCtrl',
    ['$scope', '$state', '$translate', 'uploadFileService',
        'confirmDialogService', 'bookService', 'status', 'book', 'toasterService',
        function ($scope, $state, $translate, uploadFileService,
                  confirmDialogService, bookService, status, book, toasterService) {
            $scope.status = status;
            $scope.book = book;

            // complete
            $scope.complete = function () {
                if (!$scope.form.$valid) {
                    return;
                }

                if (!$scope.form.$dirty) {
                    // no modify trades not changed
                    $state.go('app.book.list');
                    toasterService.pop('info', null, $translate.instant('strings.book.update.noUpdate'));
                    return;
                }
                $scope.filterAuthorTranslatorTag($scope.book);
                bookService.update($scope.book).then(function (data) {
                    $state.go('app.book.list');
                    toasterService.pop('success', null, $translate.instant('strings.book.update.success'));
                }, function (error) {
                    toasterService.pop('error', null, $translate.instant('strings.book.update.failure'));
                });
            };
        }]);